package com.platform.service.impl;

import com.google.common.collect.Lists;
import com.platform.annotation.SysLog;
import com.platform.base.BaseService;
import com.platform.common.Const;
import com.platform.common.ServerResponse;
import com.platform.dao.PermissionMapper;
import com.platform.dao.RolePermissionMapper;
import com.platform.pojo.RolePermission;
import com.platform.service.IRolePermissionService;
import com.platform.util.NumberUtil;
import com.platform.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;


@Service("iRolePermissionService")
public class RolePermissionServiceImpl extends BaseService implements IRolePermissionService {


    @Override
    @SysLog("新增角色操作权限")
    public ServerResponse<String> insert(Map param) {
        //param 参数为多选项权限值，
        //获取角色编码
        String roleCode = StringUtil.valueOfString(param.get("roleCode"));
        if(StringUtil.isEmpty(roleCode)){
            return ServerResponse.createByErrorMessage("参数错误，角色编码不能为空");
        }
        //根据角色编码删除以前权限数据
        rolePermissionMapper.delByRoleCode(roleCode);
        String[] pidList = StringUtil.valueOfString(param.get("")).replaceFirst("，",",")
                .split(",");
        if(null != pidList && 0 < pidList.length){
            for(String pid : pidList){
                RolePermission permission = new RolePermission();
                permission.setRoleCode(roleCode);
                permission.setPermissionId(NumberUtil.safeToNumber(pid));
                rolePermissionMapper.insert(permission);
            }
        }
        return ServerResponse.createBySuccessMessage(Const.Msg.SAVESUCCESS);
    }

    @Override
    @SysLog("删除角色操作权限")
    public ServerResponse<String> del(Integer id) {
        int countResult = rolePermissionMapper.deleteByPrimaryKey(id);
        if(countResult > 0){
            return ServerResponse.createBySuccessMessage(Const.Msg.DELSUCCESS);
        }
        return ServerResponse.createByErrorMessage(Const.Msg.DELERROR);
    }

    @Override
    @SysLog("查询角色已有操作权限")
    public ServerResponse<List<Integer>> selectByParam(String roleCode) {
        List<Integer> resultList = rolePermissionMapper.selectPidByRoleCode(roleCode);
        if(null != resultList && 0 < resultList.size()){
            return ServerResponse.createBySuccess(Const.Msg.SELECTSUCCESS,resultList);
        }
        return ServerResponse.createByErrorMessage(Const.Msg.SELECTERROR);
    }

    @Override
    @SysLog("更新角色操作权限信息")
    public ServerResponse<String> update(RolePermission rolePermission) {
        if(NumberUtil.safeToNumber(rolePermission.getId()) <= 0){
            return ServerResponse.createByErrorMessage("数据错误");
        }
        int countResult = rolePermissionMapper.updateByPrimaryKeySelective(rolePermission);
        if(countResult > 0){
            return ServerResponse.createBySuccessMessage(Const.Msg.UPDATESUCCESS);
        }
        return ServerResponse.createByErrorMessage(Const.Msg.UPDATEERROR);
    }

    @SysLog("角色操作权限查询")
    public ServerResponse<List<Map>> selectAllPermission(String roleCode){
        //返回的List
        List<Map> resultList = Lists.newArrayList();
        //先查询所有操作权限
        List<Map> perList = permissionMapper.selectByParam("");
        //查询出角色编码已有的权限
        String pidArr = permissionMapper.selectPidByRoleCode(roleCode)+",";
        if(null != perList && 0 < perList.size()){
            Map map = new HashMap();
            map.put("text","全选");
            map.put("href","");
            map.put("tags","");
            List<Map> nodeList = Lists.newArrayList();
            for (int i = 0; i < perList.size(); i++) {
                Map nodeMap = new HashMap();
                String id = StringUtil.valueOfString(perList.get(i).get("id")+",");
                if(pidArr.contains(id)){
                    nodeMap.put("have",1);
                }else{
                    nodeMap.put("have",2);
                }
                nodeMap.put("text",perList.get(i).get("name"));
                nodeMap.put("href","");
                nodeMap.put("tags",perList.get(i).get("id"));
                nodeList.add(nodeMap);

            }
            map.put("nodes",nodeList);
            resultList.add(map);
        }
        return ServerResponse.createBySuccess(Const.Msg.SELECTSUCCESS,resultList);
    }

    /**
     * 角色操作之操作权限保存
     * @return
     */
    @SysLog("新增角色操作权限")
    public ServerResponse<String> addRolePermission(String roleCode,String perIdArr){
        int resultCount = 0;
        //先删除角色所有操作权限
        rolePermissionMapper.delByRoleCode(roleCode);
        //保存操作权限集
        if(StringUtil.isNotEmpty(perIdArr)){
            //分割操作权限ID集合
            String[] ids = perIdArr.replaceAll("，",",").split(",");
            if(null != ids && 0 < ids.length){
                for (int i = 0; i <ids.length ; i++) {
                    //循环保存角色菜单
                   RolePermission permission = new RolePermission();
                   permission.setRoleCode(roleCode);
                   permission.setPermissionId(NumberUtil.safeToNumber(ids[i]));
                    if(rolePermissionMapper.insert(permission)> 0){
                        resultCount++;
                    }
                }
            }
        }
        return ServerResponse.createBySuccessMessage(Const.Msg.SAVESUCCESS);
    }

}
